#include #include #include #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define ENCODER_CLK 2 #define ENCODER_DT 3 #define ENCODER_SW 4 int volume = 0; int lastCLK = LOW; void setup() { pinMode(ENCODER_CLK, INPUT); pinMode(ENCODER_DT, INPUT); pinMode(ENCODER_SW, INPUT_PULLUP); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); updateDisplay(); } void loop() { int currentCLK = digitalRead(ENCODER_CLK); if (currentCLK != lastCLK && currentCLK == 1) { if (digitalRead(ENCODER_DT) != currentCLK) { volume = min(100, volume + 5); } else { volume = max(0, volume - 5); } updateDisplay(); } lastCLK = currentCLK; if (digitalRead(ENCODER_SW) == LOW) { volume = 0; updateDisplay(); delay(50); } } void updateDisplay() { display.clearDisplay(); display.setCursor(0, 0); display.print("Volume: "); display.print(volume); display.print("%"); int barWidth = map(volume, 0, 100, 0, SCREEN_WIDTH); display.fillRect(0, 20, barWidth, 10, WHITE); display.drawRect(0, 20, SCREEN_WIDTH, 10, WHITE); display.display(); }